package com.webvihar.access2db.components;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.webvihar.access2db.R;


public class CustomEditText extends BaseCustomComponent implements OnKeyListener{

	public enum DATATYPE {
		INTEGER, VARCHAR
	};
	
	public Context mContext;
	View view;
	EditText dataTxt;
	TextView titleTv;
	DATATYPE datatype;
	
	
	public CustomEditText(Context context) {
		super(context);
		mContext = context;
		setupLayout();
	}
	
	public CustomEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setupLayout();
		setListeners();
	}
	
	private void setupLayout() {
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.custom_component_edit_text, this);
		dataTxt = (EditText) view.findViewById(R.id.custom_edit_text_data_txt);
		titleTv = (TextView) view.findViewById(R.id.custom_edit_text_title_tv);
	}
	
	private void setListeners() {
		dataTxt.setOnKeyListener(this);
	}
	
	public int getId() {
		return super.getId();
	}
	
	public void setId(int id) {
		super.setId(id);
	}
	
	public void setType(DATATYPE datatype) {
		this.datatype = datatype;
	}
	
	public String getTitleText() {
		return titleTv.getText().toString();
	}
	
	public void setTitleText(String title) {
		titleTv.setText(title);
	}
	
	public String getDataText() {
		return dataTxt.getText().toString();
	}
	
	public void setDataText(String data) {
		dataTxt.setText(data);
	}
	
	public TextView getTitle() {
		return titleTv;
	}
	
	public EditText getEditType() {
		return dataTxt;
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// allow numbers
		if(datatype == DATATYPE.VARCHAR) {
		if(dataTxt.getText().toString().contains("1"))
			Toast.makeText(mContext, "Enter char string", Toast.LENGTH_LONG).show();
		}
		return false;
	}
}
